/* Copyright (C) 2013 Dr2co
 *
 * Created with IntelliJ IDEA.
 * User: Dr2co
 * Date: 01.07.13
 *
 *  pt-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  pt-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with pt-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.cron;

import com.aionengine.gameserver.ai2.AI2Engine;
import com.aionengine.gameserver.ai2.AIState;
import com.aionengine.gameserver.ai2.NpcAI2;
import com.aionengine.gameserver.ai2.manager.WalkManager;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.model.templates.cron.actions.spawn.CronSpawnModel;
import com.aionengine.gameserver.model.templates.cron.actions.spawn.CronSpawnPosition;
import com.aionengine.gameserver.model.templates.spawns.SpawnTemplate;
import com.aionengine.gameserver.spawnengine.SpawnEngine;
import com.aionengine.gameserver.utils.ThreadPoolManager;
import javolution.util.FastList;

/**
 * @author Dr2co
 */


public class SpawnCronService {

    public static void start(final CronSpawnModel model) {
        for (FastList.Node<CronSpawnPosition> n = model.getPosition().head(), end = model.getPosition().tail(); (n = n.getNext()) != end; ) {
            SpawnTemplate template = SpawnEngine.addNewSingleTimeSpawnCron(model.getMapId(), model.getNpcId(), n.getValue().getX(), n.getValue().getY(), n.getValue().getZ(), n.getValue().getH(), n.getValue().getRandomWalk());
            template.setStaticId(n.getValue().getStaticId());
            final Npc object = (Npc) SpawnEngine.spawnObject(template, 1);
            if (n.getValue().getAi() != null) {
                AI2Engine.getInstance().setupAI(n.getValue().getAi(), object);
                ((NpcAI2) object.getAi2()).setStateIfNot(AIState.IDLE);
            }
            if (n.getValue().getRandomWalk() > 0) {
                /*if (object.getObjectTemplate().getStatsTemplate().getWalkSpeed() == 0) {
                    object.getObjectTemplate().getStatsTemplate().setRunSpeed(CreatureSpeeds.getRunSpeed());
                    object.getObjectTemplate().getStatsTemplate().setWalkSpeed(1);
                }*/
                WalkManager.startWalking(((NpcAI2) object.getAi2()));
            }
            if (n.getValue().getWalkerId() != null) {
                object.getSpawn().setWalkerId(n.getValue().getWalkerId());
                WalkManager.startWalking((NpcAI2) object.getAi2());
            }
            if (n.getValue().getDespawnTime() > 0) {
                ThreadPoolManager.getInstance().schedule(new Runnable() {
                    @Override
                    public void run() {
                        if (object != null && object.isSpawned() && !object.getLifeStats().isAlreadyDead()) {
                            object.getController().delete();
                        }
                    }
                }, n.getValue().getDespawnTime() * 1000);
            }
        }
    }
}
